package com.example.demo.controller;

import com.example.demo.dto.StudentCourseInfo;
import com.example.demo.entity.Student;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
@CrossOrigin
public class StudentController {

    @Autowired
    private StudentService studentService;

    // 新增学生
    @PostMapping
    public int insertStudent(@RequestBody Student student) {
        return studentService.insertStudent(student);
    }

    // 删除学生
    @DeleteMapping("/{id}")
    public int deleteStudentById(@PathVariable int id) {
        return studentService.deleteStudentById(id);
    }

    // 修改学生
    @PutMapping
    public int updateStudent(@RequestBody Student student) {
        return studentService.updateStudent(student);
    }

    // 查询学生
    @GetMapping("/{id}")
    public Student selectStudentById(@PathVariable int id) {
        return studentService.selectStudentById(id);
    }

    // 查询所有学生
    @GetMapping("list")
    public List<Student> selectAllStudents() {
        return studentService.selectAllStudents();
    }

    // 联合查询学生及其选课信息
    @GetMapping("/{id}/courses")
    public Student selectStudentWithCourses(@PathVariable int id) {
        return studentService.selectStudentWithCourses(id);
    }
    @GetMapping("/course")
    public List<StudentCourseInfo> getCoursesByStudentId() {
        return studentService.getCoursesByStudentId(3);
    }
    @PostMapping("/addCourse/{id}")
    public void addCourse(@PathVariable("id") int id){
        studentService.addCourse(id);
    }
}
